//
//  ViewController.m
//  nmrsc
//
//  Created by xumckay on 16/1/9.
//  Copyright © 2016年 bric. All rights reserved.
//

#import "RootViewController.h"
#import "VersionsCheckView.h"
#define kButtonWidth    kDeviceWidth/4 //每个button的宽度

@interface RootViewController ()

@property (strong,nonatomic)NSArray *barTitleArray;   //底部bar上的文字
@property (strong,nonatomic)NSArray *navTitleArray;    //nav上的文字
@property (strong,nonatomic)NSArray *normalImageArray;  //普通状态bar图片
@property (strong,nonatomic)NSArray *selectedImageArray;  //选中状态bar的图片
@property (strong,nonatomic)UIButton *messageBtn;        //第四个主机界面的消息按钮
@property (strong,nonatomic)UIButton *zuceBtn;            //主页第四个注册按钮


@end

@implementation RootViewController

-(instancetype)init
{
    if (self=[super init]) {
        _barTitleArray=@[@"首页",@"分类",@"购物车",@"我的"];
        _navTitleArray=@[@"首页",@"分类",@"购物车",@"我的"];
        _normalImageArray=@[@"first-1",@"second-1",@"third-1",@"fourth-2"];
        _selectedImageArray=@[@"first-2",@"second-2",@"third-2",@"fourth-1"];
        _rootButtons=[[NSMutableArray alloc]init];
    }
    return self;
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    NAVBAR(@"首页");
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hidzucebtn) name:@"hidzucebtn" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hidzucebtn) name:@"hiddenSelf" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loginOutBtnClicked) name:@"loginOutBtnClicked" object:nil];
    
    
    //可滑动的底部
    _rootScroller=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width,kDeviceHeight-64)];
    _rootScroller.contentSize=CGSizeMake(kDeviceWidth*4,kDeviceHeight-64);
    _rootScroller.bounces=NO;
    _rootScroller.pagingEnabled=YES;
    _rootScroller.alwaysBounceHorizontal = YES;
    _rootScroller.showsHorizontalScrollIndicator = NO;
    _rootScroller.delegate=self;
    [self.view addSubview:_rootScroller];
    _lastclickedButtonOfTag=0;
    [self addViewControllers];
    [self addTabBarButtons];
    [self checkCanUpdate];
    
}

-(void)addViewControllers
{
    _homeVC=[[ViewController1 alloc]init];
    _homeVC.view.frame=CGRectMake(0,0,_rootScroller.width,_rootScroller.height-64);
    [_rootScroller addSubview:_homeVC.view];
    [self addChildViewController:_homeVC];
    
    _SecondVC=[[ClassifyViewController alloc]init];
    _SecondVC.view.frame=CGRectMake(_rootScroller.width,0,_rootScroller.width,_rootScroller.height);
    [_rootScroller addSubview:_SecondVC.view];
    [self addChildViewController:_SecondVC];
    
    _ThirdVC=[[ViewController3 alloc]init];
    _ThirdVC.view.frame=CGRectMake(_rootScroller.width*2,0,_rootScroller.width,_rootScroller.height);
    [_rootScroller addSubview:_ThirdVC.view];
    [self addChildViewController:_ThirdVC];
    
    //------此处需要修改
    _FourthVC=[[MyPersonCenterViewController alloc]init];
    _FourthVC.view.frame=CGRectMake(_rootScroller.width*3,0,_rootScroller.width,_rootScroller.height);
    [_rootScroller addSubview:_FourthVC.view];
    [self addChildViewController:_FourthVC];
    
    if (![[HLManager sharedManager]isLoginSuccess]) {
        if (!_loginVC) {
            _loginVC=[[LoginViewController alloc]init];
            _loginVC.view.frame=CGRectMake(_rootScroller.width*3,0,_rootScroller.width,_rootScroller.height);
        }
        [_rootScroller addSubview:_loginVC.view];
        [self addChildViewController:_loginVC];
    }
    else
    {
        [[NSNotificationCenter defaultCenter]postNotificationName:kGetSelfInfo object:nil];
    }
}

-(void)checkCanUpdate
{
    AFNetworkReachabilityManager *netWorkReachability = [AFNetworkReachabilityManager sharedManager];
    [netWorkReachability startMonitoring];
    
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:{
                NSLog(@"无网络");
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWiFi:{
                NSLog(@"WiFi网络");
                if (KversonIsAppstore == NO) {
                    [self performSelector:@selector(checkCompulsory_version) withObject:nil afterDelay:2];
                }
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWWAN:{
                NSLog(@"2G,3G,4G,5G网络");
                static int first = 0;
                if (first == 0) {
                    if (KversonIsAppstore == NO) {
                        [self performSelector:@selector(checkCompulsory_version) withObject:nil afterDelay:2];
                    }
                }
                first = 1;
//                [[NSNotificationCenter defaultCenter] postNotificationName:kUpdateSelfInfo object:nil];
                break;
            }
            default:
                break;
        }
    }];
}


-(void)checkCompulsory_version
{
    [[VersionsCheckView sharedVersionCheck]checkVersions];
}


///添加底部四个按钮
-(void)addTabBarButtons
{
    _tabBarView=[[UIView alloc]initWithFrame:CGRectMake(0,kDeviceHeight-49,kDeviceWidth,49)];
    _tabBarView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:_tabBarView];
    
    UIView *sep=[[UIView alloc]initWithFrame:CGRectMake(0,0,kDeviceWidth,0.5)];
    sep.backgroundColor=[[UIColor blackColor]colorWithAlphaComponent:0.5];
    [_tabBarView addSubview:sep];
    
    for (int i= 0; i<4;i++) {
        
        RootButton *button = [RootButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(kButtonWidth * i, 0.5, kButtonWidth, 48.5);
        [button setImage:[UIImage imageNamed:[_normalImageArray objectAtIndex:i]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:[_selectedImageArray objectAtIndex:i]] forState:UIControlStateSelected];
        [button setTitle:[_barTitleArray objectAtIndex:i] forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont systemFontOfSize:12]];
        [button.titleLabel setTextAlignment:NSTextAlignmentCenter];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [button setTitleColor:COLOR(36, 148, 88, 1) forState:UIControlStateSelected];
        [button setExclusiveTouch:YES];
        button.tag = i;
        [button setBackgroundColor:[UIColor clearColor]];
        [button addTarget:self action:@selector(rootButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_tabBarView addSubview:button];
        [_rootButtons addObject:button];

        if (i == 0) {
            button.selected = YES;
            _homeButton = button;
        } else if (i == 1) {
            _secondButton = button;
        } else if (i == 2) {
            _thirdButton = button;
        } else if (i == 3) {
            _fourButton = button;
        }
    }
}


-(void)allButtonCancelSelected
{
    _homeButton.selected=NO;
    _secondButton.selected=NO;
    _thirdButton.selected=NO;
    _fourButton.selected=NO;
}


-(void)rootButtonClick:(UIButton *)sender
{
    if(_lastclickedButtonOfTag ==sender.tag)
    {
        return;
    }
    else if(sender.tag>3)
    {
        UIAlertView *av = [[UIAlertView alloc] initWithTitle:nil message:@"tag出错" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [av show];
        return;
    }
    else
    {
        [self allButtonCancelSelected];
        NSInteger index = sender.tag;
        
        NAVBAR([_navTitleArray objectAtIndex:index]);
        [_rootScroller setContentOffset:CGPointMake(kDeviceWidth*index,0) animated:NO];
         _lastclickedButtonOfTag=index;
        if (index==0) {
            
            _homeButton.selected=YES;
            self.navigationItem.leftBarButtonItem=nil;
            self.navigationItem.rightBarButtonItem=nil;
        }
        else if (index==1)
        {
            _secondButton.selected=YES;
            self.navigationItem.leftBarButtonItem=nil;
            self.navigationItem.rightBarButtonItem=nil;
        }
        else if (index==2)
        {
            _thirdButton.selected=YES;
            self.navigationItem.leftBarButtonItem=nil;
            self.navigationItem.rightBarButtonItem=nil;
        }
        else if (index==3)
        {
            _fourButton.selected=YES;
            
            if ([[HLManager sharedManager]isLoginSuccess]) {
                if (!_messageBtn) {
                    _messageBtn=[[UIButton alloc]initWithFrame:CGRectMake(0,0,18,18)];
                    [_messageBtn setImage:[UIImage imageNamed:@"message"] forState:UIControlStateNormal];
                
                [_messageBtn addTarget:self action:@selector(rightBtnClicked) forControlEvents:UIControlEventTouchUpInside];
                }
                self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:_messageBtn];
                self.navigationItem.title=@"我的";
            }
            else
            {
                if (!_zuceBtn) {
                    _zuceBtn=[[UIButton alloc]initWithFrame:CGRectMake(0,0,40,15)];
                    [_zuceBtn setTitle:@"注册" forState:UIControlStateNormal];
                    [_zuceBtn setTitleColor:COLOR(100, 100, 100, 1) forState:UIControlStateNormal];
                    _zuceBtn.titleLabel.font=[UIFont systemFontOfSize:14];
                    [_zuceBtn addTarget:self action:@selector(zuceBtnClicked) forControlEvents:UIControlEventTouchUpInside];
                }
                    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:_zuceBtn];
                    self.navigationItem.title=@"登陆";
           }
        }
    }
}

#pragma mark -UIScrollViewDelegate

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView ==_rootScroller) {
        
        CGFloat x=scrollView.contentOffset.x;
        NSInteger index=x/scrollView.frame.size.width;
        
        index = index < 0 ? 0 : index;
        index = index > 3 ? 3 : index;
        
        RootButton *button = [_rootButtons objectAtIndex:index];
        [self rootButtonClick:button];
    }
}

//第四个控制器的消息按钮点击
-(void)rightBtnClicked
{
    NSLog(@"第四个控制器的消息按钮点击");
}

-(void)zuceBtnClicked
{
    _registVC=[[RegisterViewController alloc]init];
    [self.navigationController pushViewController:_registVC animated:NO];
}

-(void)hidzucebtn
{
    if (!_messageBtn) {
        _messageBtn=[[UIButton alloc]initWithFrame:CGRectMake(0,0,18,18)];
        [_messageBtn setImage:[UIImage imageNamed:@"message"] forState:UIControlStateNormal];
    }
    [_messageBtn addTarget:self action:@selector(rightBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:_messageBtn];
    self.navigationItem.title=@"我的";
}

-(void)loginOutBtnClicked
{
    if (!_zuceBtn) {
        _zuceBtn=[[UIButton alloc]initWithFrame:CGRectMake(0,0,40,15)];
        [_zuceBtn setTitle:@"注册" forState:UIControlStateNormal];
        [_zuceBtn setTitleColor:COLOR(100, 100, 100, 1) forState:UIControlStateNormal];
        _zuceBtn.titleLabel.font=[UIFont systemFontOfSize:14];
        [_zuceBtn addTarget:self action:@selector(zuceBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:_zuceBtn];
    self.navigationItem.title=@"登陆";
}

@end
